import json
import os
import sys
import traceback

import facade
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "EI", "download", "doi")

sqlList = []
nCount = 0


# 解析目标文件
configfile = BaseDir.get_new_path(curPath, "db.ini")
logger = facade.get_streamlogger()
mysqlutils = None
# def MajorDbConnect():
#     """
#     连接数据库 通过读取配置文件连接,如果读取配置文件 失败  返回None
#     :return:
#     """
#     return MySqlDbConnect(curPath, "db.ini")


def InsertIntoDb():
    global sqlList, nCount,mysqlutils
    newsql = []
    dicts = {}
    for count, doi in sqlList:
        if count in dicts.keys():
            dicts[count] = dicts[count] + doi + ";"
        else:
            dicts[count] = doi + ";"
    for key in dicts:
        if dicts[key]:
            listvalue = dicts[key].split(";")[:-1]
            if len(listvalue) == 1:
                listvalue.append("test")
            if key == '0':
                # 用状态2标识是下载下来解析成了0 而不是其他原因 比如返回404 或者代理失败造成的状态是0
                sql = "update `article` set `cited_cnt`={},cc_stat=2 where `doi` in {}".format(key, tuple(listvalue))
            else:
                sql = "update `article` set `cited_cnt`={},cc_stat=1 where `doi` in {}".format(key, tuple(listvalue))
            newsql.append(sql)

    # conn = MajorDbConnect()
    # ExeSqlList(newsql, conn, errExit=True)
    mysqlutils.ExeSqlListToDB(newsql)
    sqlList = list()


def AnalysisFile(filePath, fileName):
    global sqlList

    filename = os.path.splitext(fileName)[0]
    filename = filename.replace("~", "/")

    jsonstrings = BaseDir.single_read_file(filePath)
    try:
        dicts = json.loads(jsonstrings)
    except:
        print("解析错误")
        traceback.print_exc()
        print(filePath)
        sys.exit(-1)
    sort_count = dicts["sort_count"]
    if "citation" in sort_count:
        count = sort_count["citation"]["total"]
        sqlList.append((str(count), filename))
    else:
        sqlList.append(('0', filename))

    if len(sqlList) > 10000:
        InsertIntoDb()


def main():
    global mysqlutils
    mysqlutils = facade.MysqlUtiles(configfile, "db", logger=logger)
    num = 0
    print(sPath)
    for path, dirNames, fileNames in os.walk(sPath):
        for fileName in fileNames:
            filePath = os.path.join(path, fileName)
            # print(filePath)
            AnalysisFile(filePath, fileName)
            os.remove(filePath)
            num += 1
            if num % 1000 == 1:
                print(filePath)
                print("完成数量" + str(num))

    InsertIntoDb()
    print("总数量数量" + str(num))


if __name__ == "__main__":
    main()
